#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Fix encoding issues in files
"""

import os
import sys
from pathlib import Path

def fix_file_encoding(file_path):
    """Fix encoding issues in a file"""
    try:
        # Try to read with utf-8
        with open(file_path, 'r', encoding='utf-8') as f:
            content = f.read()
        
        # Write back with utf-8
        with open(file_path, 'w', encoding='utf-8') as f:
            f.write(content)
        
        print(f"? Fixed encoding for {file_path}")
        return True
    except UnicodeDecodeError:
        try:
            # Try with gbk encoding
            with open(file_path, 'r', encoding='gbk') as f:
                content = f.read()
            
            # Write back with utf-8
            with open(file_path, 'w', encoding='utf-8') as f:
                f.write(content)
            
            print(f"? Fixed encoding for {file_path} (converted from GBK)")
            return True
        except Exception as e:
            print(f"? Failed to fix encoding for {file_path}: {e}")
            return False

def main():
    """Main function"""
    print("? Fixing file encoding issues...")
    
    # Files to check
    files_to_check = [
        'gui_app.py',
        'mystep1v2.py',
        'gui_app.spec'
    ]
    
    success_count = 0
    for file_path in files_to_check:
        if Path(file_path).exists():
            if fix_file_encoding(file_path):
                success_count += 1
    
    print(f"\n? Results: {success_count}/{len(files_to_check)} files fixed")
    
    if success_count == len(files_to_check):
        print("? All files encoding issues fixed!")
    else:
        print("?? Some files may still have encoding issues")

if __name__ == "__main__":
    main() 